﻿using System.Collections.Generic;
using MC.Warehouse.ViewModels;
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Windows;
using System.Xml;
using System.Xml.Serialization;

namespace MC.Warehouse.Core
{
    public class ToolbarManager
    {
        private static ObservableCollection<Item> _toolbarItems;
        public static ObservableCollection<Item> ToolbarItems
        {
            get { return _toolbarItems ?? (_toolbarItems = new ObservableCollection<Item>()); }
        }

        internal static void LoadToolbarItem(string ucName)
        {
            var itemSource = ToolbarSource.FirstOrDefault(p => p.Name == ucName) ??
                             ToolbarSource.FirstOrDefault(p => p.Name == Contant.HomeView);
            _toolbarItems.Clear();
            if (itemSource != null)
                foreach (var item in itemSource.Item)
                {
                    ToolbarItems.Add(item);
                }
        }

        private static ObservableCollection<Items> _toolbarSource;
        private static IEnumerable<Items> ToolbarSource
        {
            get { return _toolbarSource ?? (_toolbarSource = GetToolbarSource()); }
        }

        private static ObservableCollection<Items> GetToolbarSource()
        {
            using (var xmlReader = XmlReader.Create(ConfigurationManager.AppSettings["ToolBarData"]))
            {
                var schema = new XmlSerializer(typeof(ToolBarSchema));
                var toolbars = (ToolBarSchema)schema.Deserialize(xmlReader);
                return toolbars.Items;
            }
        }

        private static RelayCommand _command;
        public static RelayCommand Command
        {
            get { return _command ?? (_command = new RelayCommand(ToolBarAction)); }
        }

        private static void ToolBarAction(object name)
        {
            var action = (ToolBarType)Enum.Parse(typeof(ToolBarType), name.ToString());
            var viewModel = TabbarManager.UcSelected.DataContext as UserControlViewModel;
            if (viewModel == null) return;
            switch (action)
            {
                case ToolBarType.Refresh:
                    viewModel.Refresh();
                    break;
                case ToolBarType.Search:
                    viewModel.Search();
                    break;
                case ToolBarType.Clean:
                    viewModel.Clean();
                    break;
                case ToolBarType.New:
                    viewModel.New();
                    break;
                case ToolBarType.Edit:
                    viewModel.Edit();
                    break;
                case ToolBarType.Copy:
                    viewModel.Copy();
                    break;
                case ToolBarType.Save:
                    viewModel.Save();
                    break;
                case ToolBarType.Delete:
                    viewModel.Delete();
                    break;
                case ToolBarType.Print:
                    viewModel.Print();
                    break;
                case ToolBarType.SendMail:
                    viewModel.SendMail();
                    break;
                case ToolBarType.ExportExcell:
                    viewModel.ExportExcell();
                    break;
                default:
                    MessageBox.Show(string.Format("Currence version do not support toolbar command type " +
                                                  "{0}", name),  "System", MessageBoxButton.OK);
                    break;
            }
        }

        internal static void Clean()
        {
            _toolbarItems = null;
            _toolbarSource = null;
            _command = null;
        }

        internal static void Enable(ToolBarType toolBarType, bool enable)
        {
            var item = ToolbarItems.SingleOrDefault(p => p.Command == toolBarType.ToString());
            if (item != null) item.Enable = enable;
            LoadToolbarItem(TabbarManager.CurrenceTabName);
        }
    }
}
